package goodbaby.dklt.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.classic.core.utils.NumberTextAnimation;
import com.classic.core.utils.NumberUtils;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.activity.LoginActivity;
import goodbaby.dklt.bean.SleepInfo;
import goodbaby.dklt.customerview.ColorArcProgressBar;
import goodbaby.dklt.customerview.HistoryFormView;
import goodbaby.dklt.customerview.Top5ListView;

/**
 * Created by Li.z on 2016/4/29.
 * 睡眠
 */
public class SleepFragment extends HealthBaseFragment {
    public static final String TAG = "SleepFragment";

    private ColorArcProgressBar mProgressBarNumber;
    private ColorArcProgressBar mProgressBarDeepSleep;
    private ColorArcProgressBar mProgressBarLittleSleep;
    private TextView tvSleepNumber;
    private TextView tvDeepSleep;
    private TextView tvLittleSleep;
    private TextView tvSleepTime;
    private TextView tvLevel;

    private Top5ListView listview;
    private HistoryFormView historyFormView;

    public static boolean shouldLoadData = true;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_sleep;
    }

    @Override
    public void initView(View parentView) {
        super.initView(parentView);

        mProgressBarNumber = (ColorArcProgressBar) parentView.findViewById(R.id.cap_sleep_number);
        mProgressBarDeepSleep = (ColorArcProgressBar) parentView.findViewById(R.id.cap_sleep_deep);
        mProgressBarLittleSleep = (ColorArcProgressBar) parentView.findViewById(R.id.cap_sleep_litt);

        tvSleepNumber = (TextView) parentView.findViewById(R.id.tv_sleep_number);
        tvDeepSleep = (TextView) parentView.findViewById(R.id.tv_deep_number);
        tvLittleSleep = (TextView) parentView.findViewById(R.id.tv_little_number);
        tvSleepTime = (TextView) parentView.findViewById(R.id.tv_sleep_time);
        tvLevel = (TextView) parentView.findViewById(R.id.tv_level);

        listview = (Top5ListView) parentView.findViewById(R.id.tlv_top_list);
        historyFormView = (HistoryFormView) parentView.findViewById(R.id.hfv_step_number);

        // loadData();

    }

    public void loadData() {

        ActionHelp.getSleepInfo(getActivity(), new ObjectCallback<SleepInfo>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onSuccess(SleepInfo response) {
                Log.i(TAG, response.toString());
                showView(response);
            }

            @Override
            public Type getType() {
                return new TypeToken<SleepInfo>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(getActivity(), "请求错误" + code);
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(getActivity(), "请求超时");
            }
        });

    }

    private float startAngle = 270;
    private float sweepAngle = 0;

    private void showView(SleepInfo info) {
        int sleepScore = NumberUtils.str2Int(info.getScore());
        float deepSleepTime = NumberUtils.str2Float(info.getFastSleepStr());
        float littleSleepTime = NumberUtils.str2Float(info.getShallowSleep());

        NumberTextAnimation.runInt(sleepScore, tvSleepNumber);
        NumberTextAnimation.runFloat(tvDeepSleep, deepSleepTime, 1, "%s分钟");
        NumberTextAnimation.runFloat(tvLittleSleep, littleSleepTime, 1, "%s分钟");

        //睡眠分数
        sweepAngle = 360 * (sleepScore / 100f);
        mProgressBarNumber.sweepAnimation(startAngle, sweepAngle);

        float sleepTotal = deepSleepTime + littleSleepTime;
        float deepSweepAngle = 360 * (deepSleepTime / sleepTotal);
        float littleSweepAngle = 360 * (littleSleepTime / sleepTotal);

        //睡眠时长
        if (sleepTotal > 60) {
            tvSleepTime.setText("睡眠时长" + (int) sleepTotal / 60 + "小时" + (int) sleepTotal % 60 + "分钟");
        } else {
            tvSleepTime.setText("睡眠时长" + (int) sleepTotal + "分钟");
        }

        if (sleepScore >= 90) {
            tvLevel.setText("睡眠充足");
        } else if (sleepScore < 90 && sleepScore >= 75) {
            tvLevel.setText("睡眠良好");
        } else if (sleepScore < 75 && sleepScore >= 60) {
            tvLevel.setText("睡眠欠佳");
        } else {
            tvLevel.setText("");
        }

        mProgressBarDeepSleep.sweepAnimation(220 - deepSweepAngle / 2, deepSweepAngle);
        mProgressBarLittleSleep.sweepAnimation(220 - littleSweepAngle / 2, littleSweepAngle);


        showHistory(info.getSleepHistoryList());
        showTop5List(info.getSleepTop5List());
    }

    private void showTop5List(final List<SleepInfo.Top5User> list) {
        listview.setTitle("睡眠排行榜");
        listview.setUsernameColor("#fefefe");

        if (list == null)
            return;

        listview.setDatas(new Top5ListView.DataAdapter() {
            @Override
            public List<Top5ListView.Top5Data> getData() {
                SleepInfo.Top5User tempUser;
                List<Top5ListView.Top5Data> datas = new ArrayList<Top5ListView.Top5Data>(list.size());
                for (int i = 0; i < list.size() && i < 5; i++) {
                    tempUser = list.get(i);
                    datas.add(new Top5ListView.Top5Data(tempUser.getImagePath(), tempUser.getChildrenName(), tempUser.getScore() + ""));
                }
                return datas;
            }
        });
    }

    private void showHistory(final List<SleepInfo.HistoryData> list) {

        historyFormView.setLeftString("最近7天");

        if (list == null)
            return;

        historyFormView.setHistoryData(new HistoryFormView.DataAdapter() {
            @Override
            public List<HistoryFormView.HistoryData> getData() {
                List<HistoryFormView.HistoryData> datas = new ArrayList<HistoryFormView.HistoryData>(list.size());

                int max = 100;

                historyFormView.setRightString("标准线70分");

                for (int i = 0; i < list.size() && i < 7; i++) {
                    HistoryFormView.HistoryData data = new HistoryFormView.HistoryData();
                    data.number = (int) (8 * (NumberUtils.str2Int(list.get(i).getScore()) / (float) max));
                    data.txt = HistoryFormView.getToday(list.get(i).getCreatedOn());
                    datas.add(data);
                }

                return datas;
            }
        });

        historyFormView.setAverage(5);

        historyFormView.show();

    }


    /*@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(isVisibleToUser && shouldLoadData){
            shouldLoadData = false;
            loadData();
        }
    }
*/

}
